import mx.controls.*;
import mx.containers.*;
import odal.*;

var are_status:TextArea;
var txt_file:TextInput;
var omsData:OMSData;
var timeOffset:Number = getTimer();
var xmlSource:XML;

//xmlSource
xmlSource.onLoad = function( bSuccess:Boolean ) {
	trace( 'test.as->xmlSource.onLoad bSuccess: ' + bSuccess.toString() );
	if ( bSuccess ) {
		writeStatus( "XML geladen.", null );
		runTests();
	} else {
		writeStatus( "XMLkann nicht geladen werden.", true );
	}//else
}//onLoad

btn_load.clickHandler = function() {
//	trace( "[btn_load.clickHandler]" );
	if ( omsData.OmsDatasource.Datasource == null ) {
		loadStructure();
	} else {
		writeStatus( "XML bereits geladen.", true );
	}//else
}//clickHandler

btn_reload.clickHandler = function() {
//	trace( "[btn_reload.clickHandler]" );
	omsData.OmsDatasource = null;
	loadStructure();
}//clickHandler

function loadStructure() {
	omsData = new OMSData();
//	trace( 'txt_file.text: ' + txt_file.text );
	omsData.OmsDatasource = new OMSXmlDatasource( txt_file.text );
	trace( 'test.as->loadStructure: vor Zuweisung xmlSource' );
	xmlSource = XML( omsData.OmsDatasource.Datasource );
//'C:/dokumente/cvscheckout/openCube/org.opencube.omscube/project/odal/project/flash/project/test/xml/structure1.xml';
}//loadStructure

function runTests() {
	var omsStructure:OMSStructure = omsData.OmsStructure;
	var elements:Array = omsStructure.Elements;
	writeStatus( 'Elemente in der Struktur: ' + elements.length, null );
	var rootElement:OMSElement = omsStructure.getElementById( "1" );
	writeStatus( 'Name von root: ' + rootElement.Name, null );
	var roleElement:OMSElement = omsStructure.getElementById( "15" );
	var value:String = roleElement.getValue( 'description', 'de' );
	writeStatus( "Wert von roleElement[description][de](soll: Veranstalter) ist: " + value, ( value != "Veranstalter" ) );
	var count:Number = rootElement.getJoinedElements( 'objects' ).length
	writeStatus( "Anzahl Elemente unter root komoniert mit 'objects' (soll 3) ist: " + count, ( count != 3 ) );
	var profileElement:OMSElement = omsStructure.getElementByBookmark( "16" );
	var targetElements:Array = profileElement.getJoinedElements( 'contactAddress' );
	var addressElement:OMSElement = targetElements[ 0 ];
	writeStatus( "Id der assozierten Kontaktaddresse (soll 17) ist: " + addressElement.Id, ( addressElement.Id != 17 ) );
}//runTests

function writeStatus( p_txt:String, p_bError:Boolean ) {
	var dif:Number = getTimer() - timeOffset;
	var msg:String = "- " + "[" + String( dif ) + "] " + p_txt;
	if ( p_bError != null ) {
		var color:String = "#00FF00";
		if ( p_bError ) {
			msg = '&lt;FEHLER!&gt; ' + msg;
			color = "#FF0000";
		}//if
		msg = "<span style='color:" + color + "'>" + msg + "</span>";
	}//if
	msg += "<br>";;
	are_status.text += msg;
	timeOffset += dif;
}//writeStatus

